package com.zjy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjy.pojo.Employee;
import com.zjy.pojo.Msg;
import com.zjy.service.EmployeeService;

@Controller
public class EmployeeController {
	@Autowired
	EmployeeService employeeService;

	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	/**
	 * 新增员工
	 * @return
	 */
	@RequestMapping(value="/emp",method=RequestMethod.POST)
	@ResponseBody
	public Msg saveEmp(Employee employee) {
		employeeService.saveEmp(employee);
		return Msg.success();
	}

	@RequestMapping("/emps")
	// 此注解可以自动的把返回的数据转换为字符串。
	@ResponseBody
	public Msg getEmpsWithJson(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
		// 引入pageHelper
		PageHelper.startPage(pn, 5);// 传入页码和每页显示的数量
		List<Employee> emps = employeeService.getAll();
		// 使用pageInfo封装查询后的结果，只需要将pageInfo交给页面就可以了
		// 封装了详细的分页信息
		PageInfo page = new PageInfo(emps, 5);// 5 是连续显示5页的内容
		return Msg.success().add("pageInfo", page);
	}
	
	//@RequestMapping("/emps")
	public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		//引入pageHelper
		PageHelper.startPage(pn,5);//传入页码和每页显示的数量
		List<Employee> emps=employeeService.getAll();
		//使用pageInfo封装查询后的结果，只需要将pageInfo交给页面就可以了
		//封装了详细的分页信息
		PageInfo page=new PageInfo(emps,5);//5 是连续显示5页的内容
		model.addAttribute("pageInfo",page);
		return "list";
		
		
	}
}

